<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="供应商" prop="supplyName">
        <el-input
          v-model="queryParams.supplyName"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品码" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入商品码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="货位" prop="whName">
        <el-input
          v-model="queryParams.whName"
          placeholder="请输入货位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类" prop="categoryName">
        <el-input
          v-model="queryParams.categoryName"
          placeholder="请输入分类"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          placeholder="请选择状态"
          v-model="queryParams.status"
          @keyup.enter.native="handleQuery"
        >
          <el-option label="正常" value="0" />
          <el-option label="停用" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['supermarket:stock:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="stockList"
      :row-class-name="tableRowClassName"
    >
      <el-table-column type="index" align="center" label="序号" />
      <el-table-column label="供应商" align="center" prop="supplyName" />
      <el-table-column label="批次号" align="center" prop="batch" />
      <el-table-column label="商品码" align="center" prop="productCode" />
      <el-table-column label="商品名称" align="center" prop="productName" />
      <el-table-column label="货位" align="center" prop="whName" />
      <el-table-column label="规格" align="center" prop="unit" />
      <el-table-column label="库存数量" align="center" prop="stockNum" />
      <el-table-column label="库存总金额" align="center" prop="stockPrice" />
      <el-table-column label="采购价" align="center" prop="cjPrice" />
      <el-table-column label="分类" align="center" prop="categoryName" />

      <el-table-column
        label="生产日期"
        align="center"
        prop="manufactureDate"
        width="100px"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.manufactureDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="过期日期"
        align="center"
        prop="expirationDate"
        width="100px"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expirationDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>

      <el-table-column label="品牌" align="center" prop="productBrand" />
      <el-table-column label="产地" align="center" prop="countryOfOrigin" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listStock } from "@/api/supermarket/stock";

export default {
  name: "Stock",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存表格数据
      stockList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        productId: null,
        productName: null,
        productCode: null,
        whName: null,
        status: null,
        categoryName: null,
        whId: null,
        stockNum: null,
        stockPrice: null,
        stockZpNum: null,
        newCjPrice: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询库存列表 */
    getList() {
      this.loading = true;
      listStock(this.queryParams).then((response) => {
        this.stockList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        stockId: null,
        productId: null,
        whId: null,
        stockNum: null,
        stockPrice: null,
        stockZpNum: null,
        newCjPrice: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "supermarket/stock/export",
        {
          ...this.queryParams,
        },
        `stock_${new Date().getTime()}.xlsx`
      );
    },
    tableRowClassName({ row }) {
      if (row.status === "1") {
        return "danger-row";
      }
      return "";
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-table .danger-row {
  background: #ffcccc;
}
</style>
